package org.ivwsqii.observer;

import java.util.ArrayList;
import java.util.List;

/**
 *	真实主题类，具体的被观察者，内部维护了观察者的列表
 * @author Administrator
 *
 */
public class RealSubject implements Subject{

	private List<Observer> list;

	private String message;

	public RealSubject() {
		message = "";
		list = new ArrayList<>();
	}
	@Override
	public void registerObserver(Observer o) {
		if(!list.contains(o)) {
			list.add(o);
		}
	}

	@Override
	public void removeObserver(Observer o) {
		if(list.contains(o)) {
			list.remove(o);
		}
	}

	@Override
	public void notifyObserver() {
		for (Observer observer : list) {
			observer.update(message);
		}
	}
	public void setInfomation(String message) {
		this.message = message;
		//消息更新，通知所有观察者
		notifyObserver();
	}
}
